package com.simple.sso.client.utils;

import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 维护一个Map，存储token令牌和对应的session信息
 */
public enum SessionStorage {

    INSTANCE;

    ConcurrentHashMap<String, HttpSession> tokenAndSessionMap = new ConcurrentHashMap<>();

    public void set(String token, HttpSession session) {
        tokenAndSessionMap.put(token, session);
    }

    public HttpSession getSession(String token) {
        if (StringUtils.isEmpty(token)) {
            throw new RuntimeException("token is must be not null!");
        }

        return tokenAndSessionMap.get(token);
    }

    public HttpSession removeAndGetSession(String token){
        if (StringUtils.isEmpty(token)) {
            throw new RuntimeException("token is must be not null!");
        }

        return tokenAndSessionMap.remove(token);
    }
}
